package com.greenkode.registration.ejb.eao;

import java.util.ArrayList;
import java.util.List;

//import javax.ejb.LocalBean;
//import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.greenkode.registration.ejb.entity.Address;
import com.greenkode.registration.ejb.entity.Group;
import com.greenkode.registration.ejb.entity.User;
import com.greenkode.registration.managedbeans.RegistrationBean;
import com.greenkode.registration.util.Util;
import javax.faces.bean.ApplicationScoped;

/**
 * Session Bean implementation class RegistrationEAO
 */
@ApplicationScoped
public class RegistrationEAO {
 
//    @PersistenceContext()
//    EntityManager entityManager;
 
    /**
     * Default constructor.
     */
    public RegistrationEAO() {
 
    }
 
    public boolean persistUser(RegistrationBean registrationBean) {
    	 
        try {
            // Populate an instance of the User Entity
            User user = new User();
            user.setUsername(registrationBean.getUsername());
            user.setPassword(Util.hash(registrationBean.getPassword()));
            user.setFirstname(registrationBean.getFirstname());
            user.setLastname(registrationBean.getLastname());
            user.setBirth(registrationBean.getDob());
            user.setEmail(registrationBean.getEmail());
 
            // Populate an instance of the Address Entity
            Address address = new Address();
            address.setStreet(registrationBean.getStreet());
            address.setSuburb(registrationBean.getSuburb());
            address.setCity(registrationBean.getCity());
            address.setZip(registrationBean.getZip());
            address.setState_Province(registrationBean.getProvince());
            address.setCountry(registrationBean.getCountry());
 
            //Set the User Address
            user.setAddress(address);
 
            //Create a List of Users
            List<User> users = new ArrayList<User>();
            users.add(user);
 
            //Add the List of Users to the Address
            address.setUsers(users);
 
            // Retrieve the Group names from the database
            String[] targets = registrationBean.getSelectedGroups();
 
            // Generate JPQL String by appending the Groups that the user was
            // added
            // to
            StringBuffer buffer = new StringBuffer();
//            buffer.append("SELECT c FROM Group c WHERE c.groupname=?1");
//            int count = 2;
//            for (int x = 1; x < targets.length; x++) {
//                buffer.append(" OR c.groupname=?" + count);
//                count++;
//            }
 
            // Add the parameter values to the query
//            count = 1;
//            Query query = this.entityManager.createQuery(buffer.toString());
//            for (String target : targets) {
//                query.setParameter(count, target);
//                count++;
//            }
 
            // Fix for expression of type list needs express conversion to
            // conform
            // to conversion List
            // List groups = query.getResultList();
 
//            List<Group> groups = Util.castList(Group.class,
//                    query.getResultList());
 
            // Add user to each group
//            for (Group group : groups) {
//                group.getUsers().add(user);
//            }
 
            // Add Groups to user
//            user.setGroups(groups);
 
            //Commit data to the database
//            entityManager.persist(user);
 
            return true;
        } catch (ClassCastException e) {
            System.out.println("Class Cast Exception");
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            System.out.println("Illegal Argument Exception");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("Generic Exception");
            e.printStackTrace();
        }
 
        return false;
    }
 
}
